// Trade-Policy Dynamics: Evidence from 60 Years of U.S.-China Trade
// Alessandria,  Khan, Khederlarian, Ruhl, and Steinberg

// inputs:	trade_4dsitc2_62-18.dta
// 			HK_adjustment.dta

// outputs:	dataset_world.dta

// Construct a dataset with not only exports to the US, but also exports to other countries. 

set varabbrev off

global dir_int "../20 Intermediate files/"

use "${dir_int}trade_4dsitc2_62-18.dta", clear
keep if year>=1962 & year<=2008
merge 1:1 i_iso3 j_iso3 sitc2_4d year using "${dir_int}HK_adjustment.dta", keepusing(vadj_ijst)
drop if _merge==2
drop _merge
replace vadj_ijst = v_ijst if vadj_ijst==.
collapse (sum) v*_ijst qty_ijst (first) *rank* phase nntr_mean ntr_mean s_mean nntr_med ntr_med s_med, by(i_iso3 j_iso3 sitc2_4d year)

rename j_iso3 source
rename i_iso3 dest
rename sitc2_4d sitc

gen chn_j = source=="CHN"
gen hkg_j = source=="HKG"
gen usa_i = dest=="USA"
gen eur_i = dest=="E12"
gen ntr = source!="CAN" & dest!="CAN" & source!="MEX" & dest!="MEX" & source!="SOV" & dest!="SOV"

bysort source dest sitc: egen temp = sum(v_ijst/(year<2001))
gen sample_pre2000_ijs = temp>0
drop temp*

// Transform variables
foreach v of varlist v* {
	gen l`v' = ln(`v') 
}
foreach v of varlist *_me* {
	gen l`v' = ln(1+`v') 
}
gen postWTO = year>2000

encode source, gen(sourcen)
encode dest, gen(destn)
encode sitc, gen(sitcn)
gen sitc1 = substr(sitc,1,1)
gen sitc2 = substr(sitc,1,2)
gen sitc3 = substr(sitc,1,3)
encode sitc1, gen(sitc1n)
encode sitc2, gen(sitc2n)
encode sitc3, gen(sitc3n)
egen id = group(source dest sitc)

save "${dir_int}dataset_world.dta", replace

